package com.jlu.springsecurityoauth2.service;

import com.jlu.springsecurityoauth2.pojo.Consumer;
import com.jlu.springsecurityoauth2.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class UserService implements UserDetailsService {


  @Autowired
  public PasswordEncoder passwordEncoder;

  @Autowired
  private MyUserService myUserService;

  @Override
  public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//    String password = passwordEncoder.encode("112233");
    Consumer consumer = myUserService.find(username);
    String name = consumer.getUsername();
    String password = consumer.getPassword();
    return new User(name,passwordEncoder.encode(password), AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
//    return new org.springframework.security.core.userdetails.User("admin",password,AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
  }
}
